﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EasyClassServer.Net;

namespace EasyClassServer.Application.GetUserInfo
{
    class GetUserInfo : IApplication
    {
        public string Name => "getinfo";

        public HttpResponseArgs Handle(HttpArgs args)
        {
            GetUserInfoModel model = new GetUserInfoModel();
            var email = args.GetPostValue()["email"];
            var token = args.GetPostValue()["token"];
            var right_token = Data.CacheManager.GetCache(email.ToString());
            if(right_token==null||right_token.ToString()!=token.ToString())
            {
                return new HttpResponseArgs
                {
                    Code = HttpCode.Error,
                    Message = "Token验证失败",
                    Data = null
                };
            }
            if(!model.Find("email",email.ToString()))
            {
                return new HttpResponseArgs
                {
                    Code = HttpCode.Error,
                    Message = "用户不存在",
                    Data = null
                };
            }
            var info = model.Select("email", email.ToString());
            if (info.Count == 0)
            {
                return new HttpResponseArgs
                {
                    Code = HttpCode.Error,
                    Message = "用户不存在",
                    Data = null
                };
            }
            return new HttpResponseArgs
            {
                Code = HttpCode.Sucsess,
                Message = "用户信息获取成功",
                Data = new GetUserInfoModel
                {
                    Class = info[0]["class"].ToString(),
                    School = info[0]["school"].ToString(),
                    Sex = info[0]["sex"].ToString(),
                    Subject = info[0]["subject"].ToString()
                }
            };
        }
    }
}
